/**
 * Project Name:Bee
 * File Name:SelectBankPopupWindow.java
 * Package Name:com.longcai.p2p.bee.view
 * Date:2015-10-26上午9:54:27
 * Copyright (c) 2015, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.lc.user.express.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.adapter.PopupAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.model.BankModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:SelectBankPopupWindow
 * @Function: TODO 
 * @Date:     2015-10-26 上午9:54:27
 * @author   Administrator
 * @version  
 * @since    JDK 1.6	 
 */
@SuppressLint("ViewConstructor")
public class SelectBankPopupWindow extends PopupWindow{
    private View mMenuView;
    private ListView listview;
    private ImageView iv_close;
    private List<BankModel> list = new ArrayList<BankModel>();
    private PopupAdapter adapter;
    private OnSelectListener onSelectListener;
    private TextView tv_title;

    public SelectBankPopupWindow(Activity context, List<BankModel> mlist, String title, int type) {
        super(context);
        list = mlist;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.layout_bank_popup_window, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) mMenuView);

        tv_title = (TextView)mMenuView.findViewById(R.id.tv_title);
        tv_title.setText(title);
        iv_close = (ImageView)mMenuView.findViewById(R.id.iv_close);
        listview = (ListView) mMenuView.findViewById(R.id.listView);
        adapter = new PopupAdapter(context, list,type);
        listview.setAdapter(adapter);
        
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int heightdown = mMenuView.findViewById(R.id.pop_layout).getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                    if (y > heightdown) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        iv_close.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                
                // TODO Auto-generated method stub
                dismiss();
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(onSelectListener != null){
                    onSelectListener.onSelect(position);
                }
            }
        });
    }
    public void refreshList(List<BankModel> list){
        if(adapter != null){
            adapter.updateListView(list);
        }
    }

    public interface OnSelectListener{
        void onSelect(int position);
    }

    public void setOnSelectListener(OnSelectListener onSelectListener){
        this.onSelectListener = onSelectListener;
    }
}

